package fragment.menufragments;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.MunicipalUnitsTitleEntity;
import fragment.AbsBaseFragment;
import utils.adapter.MunicipalUnitsTitleAdapter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/12/3.
 */
public class MunicipalUnitsFragment extends AbsBaseFragment {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MunicipalUnitsTitleAdapter adapter;
    private TextView titleForAllTv;
    private ImageView titleForAllBack;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_units_municipal;
    }

    @Override
    protected void initView() {
        tabLayout = findView(R.id.municipalUnitsTablayout);
        viewPager = findView(R.id.municipalUnitsViewPager);
        titleForAllTv = findView(R.id.titleForAllTv);
        titleForAllBack = findView(R.id.titleForAllBack);
    }

    @Override
    protected void initData() {
        titleForAllTv.setText("省市台");
        titleForAllBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        adapter = new MunicipalUnitsTitleAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        initNetwork();
    }

    private void initNetwork() {
        String titleUrl = "http://live.ximalaya.com/live-web/v1/getProvinceList?device=android";
        VolleySingleton.getInstance().addRequest(titleUrl,
                MunicipalUnitsTitleEntity.class, new Response.Listener<MunicipalUnitsTitleEntity>() {
                    @Override
                    public void onResponse(MunicipalUnitsTitleEntity response) {
                        if (response != null) {
                            List<MunicipalUnitsTitleEntity.ResultEntity> list = response.getResult();
                            if (list != null) {
                                adapter.setList(list);
                                tabLayout.setupWithViewPager(viewPager);
                                tabLayout.setTabsFromPagerAdapter(adapter);
                                dismissDialog();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }

                });
        showDialog();
    }
}
